#!/bin/bash
#
# MTAP_COMMAND        0x07
# MTAP_SW_MTAP        0x04
# MTAP_SW_ETAP        0x05
# MTAP_IDCODE         0x01
#
# MCHP_STATUS         0x00
# MCHP_ASSERT_RST     0xd1
# MCHP_DE_ASSERT_RST  0xd0
# MCHP_ERASE          0xfc
# MCHP_FLASH_ENABLE   0xfe
# MCHP_FLASH_DISABLE  0xfd
#
# ETAP_ADDRESS        0x08
# ETAP_DATA           0x09
# ETAP_CONTROL        0x0a
# ETAP_EJTAGBOOT      0x0c
# ETAP_FASTDATA       0x0e
#
#

stty -F /dev/ttyUSB0 115200 -parenb clocal -crtscts -cstopb -icrnl -ixon -ixoff \
    -parmrk -onlcr -opost -isig -icanon -echo -echok -iexten -echoe -echok \
    -echoctl -echoke


# get IDCODE
./jtagprobe --nrst_state=0
./jtagprobe --setmode=0x1f,6
./jtagprobe --sendcommand=0x1
./jtagprobe --xferdata=0x0


# chip erase
./jtagprobe --nrst_state=0
./jtagprobe --setmode=0x1f,6
./jtagprobe --sendcommand=0x04
./jtagprobe --sendcommand=0x07
./jtagprobe --xferdata=0xfc000000

# move to EJTAG boot mode
./jtagprobe --nrst_state=0
./jtagprobe --setmode=0x1f,6
./jtagprobe --sendcommand=0x5
./jtagprobe --sendcommand=0x0c
./jtagprobe --nrst_state=1

# move to NORMAL boot mode
jtagprobe --nrst_state=0
jtagprobe --setmode=0xff,8
jtagprobe --nrst_state=1

# get address
./jtagprobe --sendcommand=0x08
./jtagprobe --xferdata=0x0

# get control
./jtagprobe --sendcommand=0x0a
./jtagprobe --xferdata=0x0004c000 --dispcnt

# get data
./jtagprobe --sendcommand=0x09
./jtagprobe --xferdata=0x00000000 

# move to EJTAG boot mode
./jtagprobe --nrst_state=0
./jtagprobe --setmode=0x1f,6
./jtagprobe --sendcommand=0x5
./jtagprobe --sendcommand=0x0c
./jtagprobe --nrst_state=1

# bus setup
./jtagprobe --poke=0xbf882000,0x001f0040
./jtagprobe --poke=0xbf882010,0x00000800
./jtagprobe --peek=0xbf882040
./jtagprobe --poke=0xbf882020,0x00020000
./jtagprobe --poke=0xbf882030,0x00020000

# load pe
./jtagprobe --load_pe=../target/pe/pe.hex --program=../target/test/image.hex

# init little program
./jtagprobe --device=/dev/ttyUSB0 --poke=0xa000087c,0x3c02bf88 
./jtagprobe --device=/dev/ttyUSB0 --poke=0xa0000880,0x34426140 
./jtagprobe --device=/dev/ttyUSB0 --poke=0xa0000884,0xac400000 
./jtagprobe --device=/dev/ttyUSB0 --poke=0xa0000888,0x3c02bf88 
./jtagprobe --device=/dev/ttyUSB0 --poke=0xa000088c,0x34426150 
./jtagprobe --device=/dev/ttyUSB0 --poke=0xa0000890,0xac400000 
./jtagprobe --device=/dev/ttyUSB0 --poke=0xa0000894,0x3c02bf88 
./jtagprobe --device=/dev/ttyUSB0 --poke=0xa0000898,0x34436150 
./jtagprobe --device=/dev/ttyUSB0 --poke=0xa000089c,0x240200ff 
./jtagprobe --device=/dev/ttyUSB0 --poke=0xa00008a0,0xac620000 
./jtagprobe --device=/dev/ttyUSB0 --poke=0xa00008a4,0x00000000 
./jtagprobe --device=/dev/ttyUSB0 --poke=0xa00008a8,0x1000fff7 
./jtagprobe --device=/dev/ttyUSB0 --poke=0xa00008ac,0x00000000 

# 0xbf80f400   NVMCON
# 0xbf80f410   NVMKEY
# 0xbf80f420   NVMADDR
# 0xbf80f430   NVMDATA
# 0xbf80f440   NVMSRCADDR
# NVMCON RAZ
./jtagprobe --poke=0xbf80f410,0xaa996655
./jtagprobe --poke=0xbf80f410,0x556699aa
./jtagprobe --poke=0xbf80f400,0x4000
./jtagprobe --poke=0xbf80f410,0xaa996655
./jtagprobe --poke=0xbf80f410,0x556699aa
./jtagprobe --poke=0xbf80f400,0x8000
./jtagprobe --peek=0xbf80f400

# pfm erase
./jtagprobe --poke=0xbf80f410,0xaa996655
./jtagprobe --poke=0xbf80f410,0x556699aa
./jtagprobe --poke=0xbf80f400,0x4005
./jtagprobe --poke=0xbf80f410,0xaa996655
./jtagprobe --poke=0xbf80f410,0x556699aa
./jtagprobe --poke=0xbf80f408,0x8000
./jtagprobe --poke=0xbf80f404,0x4000
./jtagprobe --peek=0xbf80f400

# page erase
./jtagprobe --poke=0xbf80f420,0x1d000000 # NVMADDR
./jtagprobe --poke=0xbf80f410,0xaa996655
./jtagprobe --poke=0xbf80f410,0x556699aa
./jtagprobe --poke=0xbf80f400,0x4004
./jtagprobe --poke=0xbf80f410,0xaa996655
./jtagprobe --poke=0xbf80f410,0x556699aa
./jtagprobe --poke=0xbf80f408,0x8000
./jtagprobe --peek=0xbf80f400

# row prog
./jtagprobe --poke=0xbf80f420,0x1d000000 # NVMADDR
./jtagprobe --poke=0xbf80f440,0x00010000 # NVMSRCADDR 
./jtagprobe --poke=0xbf80f410,0xaa996655
./jtagprobe --poke=0xbf80f410,0x556699aa
./jtagprobe --poke=0xbf80f400,0x4003
./jtagprobe --poke=0xbf80f410,0xaa996655
./jtagprobe --poke=0xbf80f410,0x556699aa
./jtagprobe --poke=0xbf80f408,0x8000
./jtagprobe --peek=0xbf80f400

#
# TRISD  0xbf8860C0
# PORTD  0xbf8860D0
# PD4  O  E
# PD5  O  RW
#
# TRISE  0xbf886100
# PORTE  0xbf886110
# PE4  IO DB4
# PE5  IO DB5
# PE6  IO DB6
# PE7  IO DB7
#
# TRISF  0xbf886140
# PORTF  0xbf886150
# PF5  O  RS
#
# move to EJTAG boot mode
./jtagprobe --enter_serial
./jtagprobe --lcd=0,0,3
./jtagprobe --lcd=0,0,2
./jtagprobe --lcd=0,0,2
./jtagprobe --lcd=0,0,0
./jtagprobe --lcd=0,0,0
./jtagprobe --lcd=0,0,f



#
# unlock OSCON
#
jtagprobe --poke=0xbf80f230,0
jtagprobe --poke=0xbf80f230,0xaa996655
jtagprobe --poke=0xbf80f230,0x556699aa

jtagprobe --peek=0xbf80f000

# disable SOSC
jtagprobe --poke=0xbf80f004,0x2
# re-enable
jtagprobe --poke=0xbf80f008,0x2
jtagprobe --peek=0xbf80f000


#reset timer 1
jtagprobe --poke=0xbf800610,0x00000000
jtagprobe --poke=0xbf800620,0x00000080

#enable timer 1
jtagprobe --poke=0xbf800600,0x00008032
jtagprobe --peek=0xbf800600

# T1IF


#rtc
jtagprobe --enter_serial
jtagprobe --poke=0xbf80f230,0
jtagprobe --poke=0xbf80f230,0xaa996655
jtagprobe --poke=0xbf80f230,0x556699aa
jtagprobe --poke=0xbf800200,0x00008008
jtagprobe --poke=0xbf800220,0x22290000
jtagprobe --poke=0xbf800230,0x11092200





